#pragma once

#include "Comm.hpp"


class HttpResponse
{
public:
    HttpResponse() : _redirect_flag(false), _statu(200) {}

    HttpResponse(int statu) : _redirect_flag(false), _statu(statu) {}

    int GetStatu() { return _statu; }

    size_t GetBodySize() { return _body.size(); }

    void Reset()
    {
        _statu = 200;
        _body.clear();
        _redirect_flag = false;
        _redirect_url.clear();
        _headers.clear();
    }

    void SetHeader(const std::string& key, const std::string& val)
    {
        _headers.insert({ key, val });
    }

    bool HasHeader(const std::string& key)
    {
        auto it = _headers.find(key);
        if (it != _headers.end())
        {
            return true;
        }
        return false;
    }

    std::string GetHeader(const std::string& key)
    {
        auto it = _headers.find(key);
        if (it != _headers.end())
        {
            return it->second;
        }
        LOG(WARNING, "HttpResponse::GetHeader No such head: %s \n", key.c_str());
        return "";
    }

    void SetContent(const std::string& body, const std::string& type = "text/html")
    {
        _body = body;
        SetHeader("Content-Type", type);
    }

    void SetRedirect(const std::string& url, int statu = 302)
    {
        _statu = statu;
        _redirect_flag = true;
        _redirect_url = url;
    }

    bool HandleClose()
    {
        if (HasHeader("Connection") == true && GetHeader("Connection") == "keep-alive") {
            return false;
        }
        return true;
    }

public:
    int _statu;
    std::string _body;

    bool _redirect_flag;
    std::string _redirect_url;

    std::unordered_map<std::string, std::string> _headers;
};

